package com.hassean.we.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.common.util.Query;
import com.hassean.we.admin.common.util.R;
import com.hassean.we.admin.common.web.BaseController;
import com.hassean.we.admin.model.entity.TbSimCharges;
import com.hassean.we.admin.model.entity.TbSimFlow;
import com.hassean.we.admin.model.entity.TbSimInfo;
import com.hassean.we.admin.service.TbSimChargesService;
import com.hassean.we.admin.service.TbSimFlowService;
import com.hassean.we.admin.service.TbSimInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yajun
 * @since 2019-03-29
 */
@Api(description = "资费接口")
@RestController
@RequestMapping("/tbSimCharges")
public class TbSimChargesController extends BaseController {
    @Autowired private TbSimChargesService tbSimChargesService;
    @Autowired private TbSimInfoService tbSimInfoService;

    @Autowired private TbSimFlowService tbSimFlowService;
    /**
    * 通过ID查询
    *
    * @param id ID
    * @return TbSimCharges
    */
    @ApiOperation(value = "通过ID查询资费" ,  notes="通过ID查询资费")
    @GetMapping("/{id}")
    public R<TbSimCharges> get(@PathVariable Long id) {
        return new R<>(tbSimChargesService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @ApiOperation(value = "分页查询资费" ,  notes="分页查询资费")
    @RequestMapping("/page")
    public R<Map<String, Object>> page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        List< Map<String, Object>> resultList = new ArrayList<>();
        Map<String,Object>resultMap = new HashMap<>();
        if (params.get("page")!=null&&!(params.get("page")).equals("")){
            Integer page = Integer.parseInt(params.get("page").toString());
            params.put("page",page);
        }else{
            Integer page = 1;
            params.put("page",page);
        }

        if (params.get("limit")!=null&&!(params.get("limit")).equals("")){
            Integer limit = Integer.parseInt(params.get("limit").toString());
            params.put("limit",limit);
        }else{
            Integer limit = 10;
            params.put("limit",limit);
        }
        if (params.get("phone")!=null&&!(params.get("phone")).equals("")){
            String phone = params.get("phone").toString();//主叫电话号码
            params.remove("phone");
            //通过电话号码获取终端
            List<Map<String, Object>> list = tbSimInfoService.selectTbSimInfoIdByPhone(phone);
            //获取主叫终端id
            if(!list.isEmpty()){
                params.put("sim_info_id",list.get(0).get("sim_info_id"));
            }

        }
        Page<TbSimCharges> page = tbSimChargesService.selectPage(new Query<>(params), new EntityWrapper<>());;
        for (int i = 0; i <page.getRecords().size() ; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("simChargesId",page.getRecords().get(i).getSimChargesId());
            map.put("messageCharges",page.getRecords().get(i).getMessageCharges());
            map.put("flowCharges",page.getRecords().get(i).getFlowCharges());
            map.put("callCharges",page.getRecords().get(i).getCallCharges());
            map.put("rent",page.getRecords().get(i).getRent());
            map.put("totalCharges",page.getRecords().get(i).getTotalCharges());
            map.put("surplusCharges",page.getRecords().get(i).getSurplusCharges());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String addTime = sdf.format(page.getRecords().get(i).getAddTime());
            map.put("addTime",addTime);
            map.put("temp",page.getRecords().get(i).getTemp());
            map.put("status",page.getRecords().get(i).getStatus());
            map.put("remark",page.getRecords().get(i).getRemark());
            map.put("advanceText",page.getRecords().get(i).getAdvanceText());
            map.put("advanceDate",page.getRecords().get(i).getAdvanceDate());
            map.put("delFlag",page.getRecords().get(i).getDelFlag());


            Long simInfoId = page.getRecords().get(i).getSimInfoId();//手机卡ID
            if(simInfoId!=null){
                TbSimInfo tbSimInfo =   tbSimInfoService.selectById(simInfoId);
                if(tbSimInfo!=null){
                    map.put("tbSimInfo",tbSimInfo);//手机卡ID

                }
            }
            EntityWrapper entityWrapper =   new EntityWrapper<>();
            entityWrapper.eq("sim_info_id",simInfoId);
            TbSimFlow tbSimFlow =  tbSimFlowService.selectOne(entityWrapper);
            if (tbSimFlow!=null){
                map.put("tbSimFlow",tbSimFlow);//手机流量
            }
            resultList.add(map);
        }
        resultMap.put("total",page.getTotal());//总条数
        resultMap.put("size",page.getSize());//每一页多少条
        resultMap.put("pages",page.getPages());//总页数
        resultMap.put("current",page.getCurrent());//起始页
        resultMap.put("resultList",resultList);

        return new R<>(resultMap);
    }

    /**
     * 添加
     * @param  tbSimCharges  实体
     * @return success/false
     */
    @ApiOperation(value = "添加资费" ,  notes="添加资费")
    @PostMapping
    public R<Boolean> add(@RequestBody TbSimCharges tbSimCharges) {
        return new R<>(tbSimChargesService.insert(tbSimCharges));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @ApiOperation(value = "删除资费" ,  notes="删除资费")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        TbSimCharges tbSimCharges = new TbSimCharges();
        tbSimCharges.setSimChargesId(id);
        tbSimCharges.setUpdateTime(new Date());
        tbSimCharges.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(tbSimChargesService.updateById(tbSimCharges));
    }

    /**
     * 编辑
     * @param  tbSimCharges  实体
     * @return success/false
     */
    @ApiOperation(value = "编辑资费" ,  notes="编辑资费")
    @PutMapping
    public R<Boolean> edit(@RequestBody TbSimCharges tbSimCharges) {
        tbSimCharges.setUpdateTime(new Date());
        return new R<>(tbSimChargesService.updateById(tbSimCharges));
    }
}
